package com.uam.core.dao.mysql;

import java.sql.*;
import java.util.List;

import com.uam.core.entity.model.TaskModel;
import com.uam.core.lk.utils.fencebuffer.BufferBase;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.uam.core.entity.constant.Constant;
import com.uam.core.entity.model.StationQueryResult;

import lombok.extern.slf4j.Slf4j;

@Repository
@Slf4j
public class StationDao {

	@Autowired
	private JdbcTemplate jdbcTemplate = null;

	public int countValidStationTotal(double minAltitude) {
		return this.jdbcTemplate.query(con -> {
			return this.generateValidStationPreparedStatement(minAltitude,
					new StringBuilder("SELECT COUNT(`point_id`) FROM `point`"), con);
		}, new ResultSetExtractor<Integer>() {
			public Integer extractData(ResultSet resultSet) throws SQLException, DataAccessException {
				if (resultSet.next()) {
					return resultSet.getInt(1);
				}
				return Integer.valueOf(0);
			}
		}).intValue();
	}

	public StationQueryResult queryValidStation(double minAltitude) {
		int total = this.countValidStationTotal(minAltitude);
		if (total == 0) {
			return StationQueryResult.EMPTY;
		}
		return new StationQueryResult(total, this.jdbcTemplate.query(con -> {
			return this.generateValidStationPreparedStatement(minAltitude,
					new StringBuilder("SELECT `position` FROM `point`"), con);
		}, new RowMapper<double[]>() {

			@Override
			public double[] mapRow(ResultSet rs, int rowNum) throws SQLException {
				int i = 0;
				try {
					String[] position = Constant.COMMA_REGEX.split(rs.getString("position").trim(), 0);
					return new double[] { Double.parseDouble(position[i++]), Double.parseDouble(position[i]) };
				} catch (Throwable e) {
					StationDao.log.error(e.getMessage(), e);
					return null;
				}
			}
		}));
	}

	private PreparedStatement generateValidStationPreparedStatement(double minAltitude, StringBuilder sql,
			Connection con) throws SQLException {
		sql.append(" WHERE `status`=1 AND `height`>=?");
		PreparedStatement ps = con.prepareStatement(sql.toString());
		ps.setInt(1, (int) minAltitude);
		sql.delete(0, sql.length());
		return ps;
	}


	public List<StationModel> queryStationModel() {
		List<StationModel> data = this.jdbcTemplate.query(con -> {
			String sql="SELECT point_id,point_name,floor,height,status,position FROM point where STATUS=1;";
			PreparedStatement ps = con.prepareStatement(sql);
			return ps;
		}, new RowMapper<StationModel>() {
			@Override
			public StationModel mapRow(ResultSet rs, int rowNum) throws SQLException {
				StationModel task = new StationModel();
				task.setPointId(rs.getInt("POINT_ID"));
				task.setPointName(rs.getString("POINT_NAME"));
				task.setFloor(rs.getInt("FLOOR"));
				task.setHeight(rs.getInt("HEIGHT"));
				task.setStatus(rs.getInt("STATUS"));
				task.setPosition(rs.getString("POSITION"));

				String[] position =task.getPosition().split("\\,");
				Point point=BufferBase.geometryFactory.createPoint(new Coordinate(Double.parseDouble(position[0]),Double.parseDouble(position[1])));
				task.setPoint(point);
				return task;
			}
		});
		return data;
	}

}
